<?
/**
 * 
 * 
 * */
class shop
{
   /**
    * 
    */
   static public function setInfo(&$db,$urs,&$ArrP,&$ArrF=array())
   {
       if($ArrP["sname"])   	 $bind["sname"] = $ArrP["sname"];
       if($ArrP["shop_class"])   $bind["shop_class"] = $ArrP["shop_class"];
       if($ArrP["cShopStyle"])   $bind["cShopStyle"] = $ArrP["cShopStyle"];
       if($ArrP["grade"])   	 $bind["grade"] = $ArrP["grade"];
       if($ArrP["summary"]) 	 $bind["summary"] = $ArrP["summary"];
       $rs = $db->row("select sid from shop_info where urs = ? ",$urs);
       if($rs)
       {
           $id = $rs["sid"];
           $db->update("shop_info",$bind," urs = ? ",$urs);
       }else
       {
          $bind["urs"] = urs;
          $bind["save_time"] = time();
          $id = $db->insert("shop_info",$bind);
       }
       if($id)
       {
            //简介
            $ArrP["desc"] && $db->replace("shop_info_desc",array("sid"=>$id,"urs"=>urs,"desc"=>$ArrP["desc"]));
            //上传图片
       		$ArrF['img']['size'] && self::modifyPctImg($id,$ArrF);
       		//返回
       		return array(1,"成功:操作成功!");
       }else
       {
           //返回
           return array(0,"失败:系统忙,请重试!");
       }
   }
   static public function getInfo(&$db_goods,$urs)
   {
        return $db_goods->row("select * from shop_info where urs = ? ",$urs);
   }
   static public function getShopInfo(&$db_goods,&$db_member,$urs)
   {
        $rs   = array();
        $tmp  = $db_member->row("select * from member_im where urs = ? ",$urs);
		unset($tmp["urs"]);
		$rs["im"] = $tmp;
		$tmp  = $db_member->row("select `reg_time`,`last_time` from `member_log` where urs = ? ",$urs);
		$rs["reg_time"]  = $tmp["reg_time"];
		$rs["last_time"] = $tmp["last_time"];
		$tmp  = self::getInfo($db_goods,$urs);
		$rs["save_time"] = $tmp["save_time"];
		$rs["goods"]     = self::getGoodsNums($db_goods,$urs);
		$rs["saleCredit"]= credit::getTotalSaleCredit($db_member,$urs);
		$rs["buyCredit"] = credit::getTotalBuyCredit($db_member,$urs);
		return $rs;
   }
   static public function getGoodsNums($db_goods,$urs)
   {
       $where = array(
       		"where" =>" where urs = :urs  and `upload` = 1 ",
       		"bind"  =>array("urs"=>$urs)
       );
       $table 		= "goods_info";
       $rs 		    = $db_goods->row("select count(gYmd) as cc from {$table} {$where['where']} ",$where['bind']);
	   return (int)$rs["cc"];    
   }
   static public function id2name($db,$id=0)
   {
       $ca = new ca("cats_shop","cats_shop",3600);
	   if(!$cats_shop=$ca->get())
	   {
	       $cats   = $db->dataArray("select * from cats_shop where pcsid>0 order by taxis");
	   	   foreach($cats as &$v)$cats_shop[$v["csid"]]=$v["name"];
		   $ca->put($cats_shop);
	   }
	   if($id)
	   {
	       return $cats_shop[$id];
	   }else
	   {
	       return $cats_shop;
	   }
   }
   static public function url($urs,$Arr=array())
   {
       $Arr["urs"] = $urs;
       return surl::en(surl::get('shop')."shop.php",$Arr);
   }
   static public function subClass($db,$urs)
   {
        $ca = new ca($urs,"subClass",3600,10000);
	    if(!$rs = $ca->get())
	    {
	        $where = array(
				"str" => " where urs = ? ",
				"bind"=>$urs
			);
			$rs = $db->dataArray("select * from shop_class_id {$where['str']} order by taxis limit 0,20",$where['bind']);
	    	$ca->get($rs);
		}
	    return $rs;
   }
   /**
    * 得到 店铺推荐位的商品
    */
   static public function shoprecommend($db,$urs)
   {
       return $db->dataArray("select * from shop_recommend_goods where urs = ?  and `upload` = 1 limit 0,6",$urs);		
   }
   static public function shoplist($db,$urs,$page,$rows,$sShopClassid=0,$q='',$order='')
   {
       $where = array(
       		"where" =>" where urs = :urs  and `upload` = 1 ",
       		"bind"=>array(
       			"urs"=>$urs,
       		)
       );
       if($sShopClassid)
       {
            $where['where'] 		.= " and sShopClassid = :sShopClassid ";
	   		$where['bind']['sShopClassid']= $sShopClassid;
       }
       if($q)
	   {
			$where['where'] 		.= "  and `sTitle` LIKE :sTitle ";
			$where['bind']['sTitle']= "%{$q}%";
	   }
	   if($order)
	   {
	       $order = $order == 'desc'?' order by gYmd desc,gid desc ':' order by  gYmd asc,gid asc ';
	   }else
	   {
	       $order = "";
	   }
	   $table 		= "goods_info";
	   $pg			= pg::sp($db,$table,$where,$page,$rows);
	   $rs 		    = $db->dataArray("select * from {$table} {$where['where']} {$order} limit {$pg['begin']},".$rows,$where['bind']);
	   //echo $db->getsql();
	   return array("pg"=>$pg,"rs"=>$rs);
   }
   static public function friendLink($db,$urs)
   {
        $ca = new ca($urs,"friendLink",3600,10000);
	    if(!$rs = $ca->get())
	    {
	        $where = array(
				"str" => " where urs = ? ",
				"bind"=>$urs
			);
			$rs = $db->dataArray("select * from shop_friendlink {$where['str']} order by taxis limit 0,20",$where['bind']);
	    	$ca->put($rs);
	    }
	    return $rs;
   }
   /**
     * 參數:$id
     * 返回:string URL
     */
    static public function img($id)
    {
        $p = $s = 0;
        if($id>1000) $s = floor($id/1000);
        if($s>100)   $p = floor($s/100);
        $pctImgFileRoot = surl::get("img")."shop{$p}/{$arr['mm']}/{$s}/";
        return $pctImgFileRoot.$id.".jpg";
    }
    
    /**
     * 功能:得到商品图片的存放的URL
     * 參數:$id
     * 返回:FileName
     */
    static public function imgLocalFileName($id)
    {
        $p = $s = 0;
        if($id>1000) $s = floor($id/1000);
        if($s>100)   $p = floor($s/100);
        $pctImgFileRoot = UP_IMG_DIR."shop{$p}/{$arr['mm']}/{$s}/";
        g::dir($pctImgFileRoot,true);
		return $pctImgFileRoot.$id.".jpg";
    }
   /**
    * 功能:改商品图片
    * 参数:
    *     $arrP
    *     $arrF
    * 返回:array
    */
   static public function modifyPctImg($id,$ArrF)
   {
        $pctImgFile = self::imgLocalFileName($id);
		if(move_uploaded_file($ArrF['img']['tmp_name'], $pctImgFile))
		{
			$rs=img::cut($pctImgFile,PCT_IMG_SW,PCT_IMG_SH,2,95);
			if($rs[0])
			{
				return array(1,"成功:图片上传成功!");
			}else
			{
				return $rs;//这里返图片里的信息就可以啦
			}
		}else
		{
			return array(0,"提示:图上传失败!");
		}
   }
   
}
?>